
<?php include_once(dirname(__FILE__) . '/../general/head.php'); ?>

<!-- header -->
<?php include_once(dirname(__FILE__) . '/../general/header.php'); ?>

<?php //include_once('left.php'); ?>


<div id="page-wrapper" style="margin-left: auto; min-height: 850px;">
    <div class="row">
        <div class="col-lg-12">
            <div style="text-align: center;">
                <h1 class="page-header"><?php echo $result->quiz_name; ?></h1>
            </div>
        </div>
    </div>
    <div class="row">

        <div class="col-lg-9"> 
            <div class="panel panel-default">
                <div class="panel-heading">
                    Danh sách câu hỏi
                </div>
                <div class="panel-body">
                    <?php foreach ($assigned_question[0] as $key => $question) { ?>
                        <div class="count_btn" id="nq<?php echo $key; ?>" onClick="showquestion('<?php echo $key; ?>');"><?php echo $key + 1; ?></div>
                    <?php } ?>
                    <br>
                    <br>
                </div>
            </div>
        </div>
            <div class="col-lg-3"> 
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                Ghi chú
                            </div>
                            <div class="panel-body">
                                <table>
                                    <tr>
                                        <td style="vertical-align: middle;"><div class="count_btn" style="background:#267B02;">&nbsp;</div> </td><td>Đã trả lời &nbsp;&nbsp; </td>
                                        <td style="vertical-align: middle;"><div class="count_btn" style="background:#D03800;">&nbsp;</div> </td><td>Chưa trả lời &nbsp;&nbsp; </td>
                                        <td style="vertical-align: middle;"><div class="count_btn" style="background:#FFD800;">&nbsp;</div> </td><td>Xem lại sau &nbsp;&nbsp; </td>
                                    </tr>
                                    
                                </table>
                                
                            </div>
                        </div>
        </div>

        <!--Phần làm bài-->
        <div class="col-lg-12"> 
            <div class="panel panel-default">                
                <div class="panel-body">
                    <div style="float: right;">
                        <p class="lead">Thời gian còn lại: <div id='timer' style="display:inline; font-size: 30px;"> </div>

                        </p>
                    </div>
                    <?php
                    $category_name = explode(",", $time_info['category_name']);
                    $cate_range = array();
                    $startnextcate = array();
                    $qids_range = explode(",", $time_info['qids_range']);
                    foreach ($qids_range as $key => $rangeval) {
                        $rangeval_arr = explode("-", $rangeval);
                        $gen_range = array();
                        $startnextcate[] = $rangeval_arr['1'];
                        for ($x = $rangeval_arr['0']; $x <= $rangeval_arr['1']; $x++) {
                            $gen_range[] = $x;
                        }
                        $cate_range[] = $gen_range;
                    }
                    ?>
                    <input type="hidden"  id="json_category_range" value='<?php echo json_encode($cate_range); ?>'>
                    <?php if (false) { ?>
                        <?php
                        foreach ($category_name as $key => $cvalue) {
                            ?>
                            <?php if ($key == '0') { ?>
                                <input type="hidden" value="0" id="current_cate">
                            <?php } ?>
                            <div class="category_box" id="cate-<?php echo $key; ?>" <?php
                            if ($key == "0") {
                                echo "style='background:#2f72b7;color:#ffffff;'";
                            }
                            ?> onClick="changecategory('<?php echo $key; ?>');"><?php echo $cvalue; ?></div>

                        <?php } ?>
                    <?php } ?>
                    <form action="<?php echo site_url(get_folder($loggedUser['title']).'/quizzes/submit_quiz/' . $quiz_id); ?>" method="post" id="testform" 
                          onsubmit="showquestion('0');return checkSubmit();">
                        <?php
                        $category_number = 0;

                        $selected_oids = explode(",", $time_info['oids']);
                        foreach ($assigned_question[0] as $key => $question) {
                            ?>
                            <?php if ($key == '0') { ?>
                                <input type="hidden" value="0" id="current_question">
                            <?php } ?>


                            <div style="border: 0px;" id="ques<?php echo $key; ?>" class="<?php
                            if ($key == '0') {
                                echo 'showquestion';
                            } else {
                                echo 'hidequestion';
                            }
                            ?>">

                                <br>
                                <p> Câu <?php echo $key + 1; ?>:</p>
                                <b><?php echo $question['question']; ?></b>
                                <br>
                                <table>
                                    <?php
                                    $opcount = 0;
                                    foreach ($assigned_question[1] as $keys => $option) {
                                        if ($option['qid'] == $question['qid']) {
                                            ?>

                                            <tr>
                                                <td style="border:0px;width:750px;"> 
                                                    <div class="radio">
                                                        <label>
                                                            <input type="radio" id="op-<?php echo $key; ?>-<?php echo $opcount; ?>" name="answers<?php echo $key; ?>" value="<?php echo $option['oid']; ?>" onClick="update_answer(this.value);
                                                                                answered_color();" <?php
                                                                   if ($selected_oids[$key] == $option['oid']) {
                                                                       echo "checked";
                                                                   }
                                                                   ?> > 
                                                                   <?php echo $option['option_value']; ?>
                                                        </label>
                                                    </div>
                                                </td>
                                            </tr>

                                            <?php
                                            $opcount+=1;
                                        }
                                    }
                                    ?>
                                </table>
                                <br><br>
                                <input type="checkbox" value="1" onClick="reviewlater();"> Đánh dấu xem lại sau
                                <br><br>
                                <?php
                                if ($key >= "1") {
                                    ?>
                                    <input class="btn btn-default" type="button" value="Câu trước"  onClick="showquestion('<?php echo $key - 1; ?>');">
                                    <?php
                                } else {
                                    ?>
                                    <input class="btn btn-default disabled" type="button" value="Câu trước">
                                    <?php
                                }

                                if ($key != (count($assigned_question['0']) - 1)) {
                                    if (in_array($key, $startnextcate)) {
                                        $category_number+=1;
                                        ?>
                                        <input type="button" class="btn btn-default" value="Câu sau"  onClick="showquestion('<?php echo $key + 1; ?>');
                                                            changecategory('<?php echo $category_number; ?>')">
                                               <?php
                                           } else {
                                               ?>

                                        <input type="button" value="Câu sau" class="btn btn-default" onClick="showquestion('<?php echo $key + 1; ?>');">

                                        <?php
                                    }
                                } else {
                                    ?>
                                    <input type="button" value="Câu sau" class="btn btn-default disabled" >
                                    <?php
                                }
                                ?>
                                &nbsp;&nbsp;&nbsp;&nbsp;

                                &nbsp;&nbsp;&nbsp;&nbsp;
    <!--                                <input type="button" onClick="clearresponse('<?php echo $key; ?>');" value="Clear Response">-->


                            </div>

                        <?php } ?>

                        <br><br>
                        <input type="hidden" name="noq" id="noq" value="<?php echo $key; ?>">
                        <div style="text-align: right;">
                            <input type="submit" class="btn btn-primary" value="NỘP BÀI">
                        </div>
                    </form>

                </div>
            </div>

        </div>




    </div>


</div>
<!-- /#page-wrapper -->
<script type="text/javascript" src="<?php echo base_url(); ?>/js/basic.js"></script>
<script>
    function checkSubmit()
    {
        var selecteds = $('input[type=radio]:checked');
        
        return confirm('Đã hoàn thành '+selecteds.length+'/'+<?php echo count($assigned_question[0]); ?>+'. Bạn chắc chắn muốn nộp bài?');
    }

                                            var Timer;
                                            var TotalSeconds;


                                            function CreateTimer(TimerID, Time) {
                                                Timer = document.getElementById(TimerID);
                                                TotalSeconds = Time;

                                                UpdateTimer()
                                                window.setTimeout("Tick()", 1000);
                                            }

                                            function Tick() {
                                                if (TotalSeconds <= 0) {
                                                    alert("Time's up!")
                                                    return;
                                                }

                                                TotalSeconds -= 1;
                                                UpdateTimer()
                                                window.setTimeout("Tick()", 1000);
                                            }

                                            function UpdateTimer() {
                                                var Seconds = TotalSeconds;

                                                var Days = Math.floor(Seconds / 86400);
                                                Seconds -= Days * 86400;

                                                var Hours = Math.floor(Seconds / 3600);
                                                Seconds -= Hours * (3600);

                                                var Minutes = Math.floor(Seconds / 60);
                                                Seconds -= Minutes * (60);


                                                var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds)


                                                Timer.innerHTML = TimeStr;
                                            }


                                            function LeadingZero(Time) {

                                                return (Time < 10) ? "0" + Time : +Time;

                                            }

//var myCountdown1 = new Countdown({time:<?php echo $seconds; ?>, rangeHi:"hour", rangeLo:"second"});
                                            setTimeout(submitform, '<?php echo $seconds * 1000; ?>');
                                            function submitform() {
                                                alert('Time Over');
                                                document.getElementById('testform').submit();
                                            }

                                            setInterval(setqtime, 1000);

                                            function setqtime() {

                                                qtime += 1;
                                            }

                                            $(document).ready(function() {
                                                hideqnobycate();
                                            });


</script>
<script type="text/javascript">window.onload = CreateTimer("timer", <?php echo $seconds; ?>);</script>

<?php include_once(dirname(__FILE__) . '/../general/footer.php'); ?>